<?php
/**
 * Copyright 2012  SURFfoundation
 * 
 * This file is part of ESCAPE.
 * 
 * ESCAPE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ESCAPE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ESCAPE.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * For more information:
 * http://escapesurf.wordpress.com/
 * http://www.surffoundation.nl/
 * 
 * Contact: d.vierkant@utwente.nl
 * 
 * @license http://www.gnu.org/licenses/gpl.html GNU GPLv3
 */
?>
<?php
/**
 * Sesame
 * 
 * Triple store implementation for SESAME 2.3+.
 */
class triplestore_sesame_Sesame extends triplestore_TripleStore
{
	protected $serverUrl = null;
	
	// parameters: "server_url"
	public function __construct(&$params)
	{
		parent::__construct($params);
		$this->serverUrl = $params['server_url'];
		$this->sparqlEndpointUrl = $this->serverUrl;
	}

	public function fetchStatements($subject, $predicate, $object, $objectIsLiteral, $graphUri)
	{
		if($object !== null && $objectIsLiteral)
		{
			// don't use this method for literal objects to avoid URL length restrictions
			return parent::fetchStatements($subject, $predicate, $object, $objectIsLiteral, $graphUri);
		}
		else
		{
			// use sesame's repository statements instead of the default SPARQL implementation (tiny bit faster?)
			$queryString = array();
			if($subject !== null)
			{
				$queryString[] = 'subj=' . rawurlencode('<' . $this->sparqlEscapeUri($subject) . '>');
			}
			if($predicate !== null)
			{
				$queryString[] = 'pred=' . rawurlencode('<' . $this->sparqlEscapeUri($predicate) . '>');
			}
			if($object !== null)
			{
				$queryString[] = 'obj=' . rawurlencode('<' . $this->sparqlEscapeUri($object) . '>');
			}
			if($graphUri !== null)
			{
				$queryString[] = 'context=' . rawurlencode('<' . $this->sparqlEscapeUri($graphUri) . '>');
			}
			
			$url = $this->serverUrl . '/statements?' . implode('&', $queryString);
			
			curl_setopt($this->curlHandle, CURLOPT_URL, $url);
			curl_setopt($this->curlHandle, CURLOPT_POST, false);
			curl_setopt($this->curlHandle, CURLOPT_HTTPHEADER, array('Accept: application/rdf+xml'));
	
			$result = curl_exec($this->curlHandle);
			if($result === false)
			{
				throw new triplestore_exception_QueryFailed('Query failed', null);
			}
			else if(curl_getinfo($this->curlHandle, CURLINFO_HTTP_CODE) != 200)
			{
				throw new triplestore_exception_QueryFailed($result, null);
			}
			
			$result = new simplerdf_RdfDocument($result);
			return $result;
		}
	}
	
	public function replaceGraph($graphUri, $rdf)
	{
		if($rdf instanceof simplerdf_RdfDocument || $rdf instanceof simplerdf_RdfResource)
		{
			$rdf = $rdf->getRdfXml();
		}
		
		$params = array(
			'http' => array(
				'method' => 'PUT',
				'header' => "Content-Type: application/rdf+xml;charset=UTF-8",
				'content' => &$rdf
				)
			);
		
		if($this->username !== null)
		{
			$params['http']['header'] = "Authorization: Basic " . base64_encode($this->username . ':' . $this->password) . "\r\n" . $params['http']['header'];
		} 
		
		$ctx = stream_context_create($params);
		@file_get_contents($this->serverUrl . '/statements?context=' . rawurlencode('<' . $graphUri . '>'), false, $ctx);
	}

	public function deleteGraph($graphUri)
	{
		$params = array(
			'http' => array(
				'method' => 'DELETE',
				'header' => "Content-Type: application/rdf+xml;charset=UTF-8",
				)
			);

		if($this->username !== null)
		{
			$params['http']['header'] = "Authorization: Basic " . base64_encode($this->username . ':' . $this->password) . "\r\n" . $params['http']['header'];
		} 
		
		$ctx = stream_context_create($params);
		@file_get_contents($this->serverUrl . '/statements?context=' . rawurlencode('<' . $graphUri . '>'), false, $ctx);
	}
}